//
//  ResultModel.swift
//  Pods
//
//  Created by eme on 2017/3/31.
//
//

import Foundation
import SwiftyJSON
import NetWorkCore
import ModelProtocol


class ResultModel: ModelProtocol {
    
    // MARK: Declaration for string constants to be used to decode and also serialize.
    internal let kResultNumberKey: String = "number"
    internal let kResultIssignKey: String = "issign"
    internal let kResultDeliverystatusKey: String = "deliverystatus"
    internal let kResultListKey: String = "list"
    internal let kResultTypeKey: String = "type"
    
    
    // MARK: 属性
    var number: String
    var issign: String
    var deliverystatus: String
    var list: [LogisticsListModel]
    var type: String
    
    
    // MARK: 实现MikerSwiftJSONAble 协议， 解析json数据
    required  init?(json: JSON) {
        number = json[kResultNumberKey].stringValue
        issign = json[kResultIssignKey].stringValue
        deliverystatus = json[kResultDeliverystatusKey].stringValue
        list = []
        if let items = json[kResultListKey].array {
            for item in items {
                if let LogisticsListModel = LogisticsListModel(json: item){
                    list.append(LogisticsListModel)
                }
            }
        } else {
            list = []
        }
        type = json[kResultTypeKey].stringValue
        
    }
    
    
    /**
     Generates description of the object in the form of a NSDictionary.
     - returns: A Key value pair containing all valid values in the object.
     */
    internal func dictionaryRepresentation() -> [String : AnyObject ] {
        
        var dictionary: [String : AnyObject ] = [ : ]
            
        dictionary.updateValue(number as AnyObject, forKey: kResultNumberKey)

        dictionary.updateValue(issign as AnyObject, forKey: kResultIssignKey)
    
        dictionary.updateValue(deliverystatus as AnyObject, forKey: kResultDeliverystatusKey)
        
        if list.count > 0 {
            var temp: [AnyObject] = []
            for item in list {
                temp.append(item.dictionaryRepresentation() as AnyObject)
            }
            dictionary.updateValue(temp as AnyObject, forKey: kResultListKey)
        }
        
        dictionary.updateValue(type as AnyObject, forKey: kResultTypeKey)
        
        return dictionary
    }

}
